#include "stdafx.h"
#include "wf_polylinedel.h"
#include "pf_polyline.h"

#include "pf_snapper.h"



WF_PolylineDel::WF_PolylineDel(PF_Container& container,
	PF_GraphicView& graphicView)
	:PF_PreviewActionInterface("Delete node",
			   container, graphicView) {}


QAction* WF_PolylineDel::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
    QAction* action = new QAction(tr("&Delete node"), NULL);
	action->setShortcut(QKeySequence());
    action->setStatusTip(tr("Delete polyline's node"));
    return action;
}

void WF_PolylineDel::init(int status) {
    PF_ActionInterface::init(status);
    delEntity = NULL;
    delPoint = PF_Vector(false);
}



void WF_PolylineDel::trigger() {

    PF_DEBUG->print("PF_ActionPolylineDel::trigger()");

	if (delEntity!=NULL && delPoint.valid &&
	    delEntity->isPointOnEntity(delPoint)) {

		delEntity->setHighlighted(false);
		graphicView->drawEntity(delEntity);

		PF_Modification m(*container, graphicView);
		delEntity = m.deletePolylineNode((PF_Polyline&)*delEntity, delPoint );

		delPoint = PF_Vector(false);

	    PF_DIALOGFACTORY->updateSelectionWidget(container->countSelected());
    }
////////////////////////////////////////2006/06/15
		graphicView->redraw();
////////////////////////////////////////
}



void WF_PolylineDel::mouseMoveEvent(PF_MouseEvent* e) {
    PF_DEBUG->print("PF_ActionPolylineDel::mouseMoveEvent begin");


    switch (getStatus()) {
    case ChooseEntity:
		break;

    case SetDelPoint:
		snapPoint(e);
		break;

    default:
		break;
    }

    PF_DEBUG->print("PF_ActionPolylineDel::mouseMoveEvent end");
}



void WF_PolylineDel::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
		switch (getStatus()) {
		case ChooseEntity:
		    delEntity = catchEntity(e);
		    if (delEntity==NULL) {
			PF_DIALOGFACTORY->commandMessage(tr("No Entity found."));
		    } else if (delEntity->rtti()!=LX_Define::EntityPolyline) {

			PF_DIALOGFACTORY->commandMessage(
			    tr("Entity must be a polyline."));
		    } else {
			    PF_Vector clickCoord = snapPoint(e);
				delEntity->setHighlighted(true);
				graphicView->drawEntity(delEntity);
				setStatus(SetDelPoint);
////////////////////////////////////////2006/06/15
				graphicView->redraw();
////////////////////////////////////////
		    }
		    break;

		case SetDelPoint:
		    delPoint = snapPoint(e);
		    if (delEntity==NULL) {
				PF_DIALOGFACTORY->commandMessage(tr("No Entity found."));
		    } else if (!delPoint.valid) {
				PF_DIALOGFACTORY->commandMessage(tr("Adding point is invalid."));
		    } else if (!delEntity->isPointOnEntity(delPoint)) {
				PF_DIALOGFACTORY->commandMessage(
				    tr("Adding point is not on entity."));
		    } else {
				deleteSnapper();
				trigger();
		    }
		    break;

		default:
		    break;
		}
    } else if (e->button()==Qt::RightButton) {
		deleteSnapper();
		if (delEntity!=NULL) {
	    	delEntity->setHighlighted(false);
	    	graphicView->drawEntity(delEntity);
////////////////////////////////////////2006/06/15
			graphicView->redraw();
////////////////////////////////////////
		}
		init(getStatus()-1);
    }
}

void WF_PolylineDel::updateMouseButtonHints() {
    switch (getStatus()) {
    case ChooseEntity:
		PF_DIALOGFACTORY->updateMouseWidget(tr("Specify polyline to delete node"),
					    tr("Cancel"));
		break;
    case SetDelPoint:
		PF_DIALOGFACTORY->updateMouseWidget(tr("Specify deleting node's point"),
					    tr("Back"));
		break;
    default:
		PF_DIALOGFACTORY->updateMouseWidget("", "");
		break;
    }
}



void WF_PolylineDel::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_PolylineDel::updateToolBar() {
    switch (getStatus()) {
    case SetDelPoint:
    case ChooseEntity:
		PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
		break;
    default:
		PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarPolylines);
		break;
    }
}


// EOF
